﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
//using System.Drawing.Text;

public class CopyPath : MonoBehaviour
{
    [MenuItem("GameObject/复制路径 &v", false, 0)]
    static void CopyGameObjectPath()
    {
        Transform select = Selection.activeTransform;
        string _name = select.gameObject.name;
        Transform tmp = null;
        List<string> strList = new List<string>();
        if (null != select.parent)
        {
            tmp = select;
            while (tmp != null)
            {
                strList.Insert(0, tmp.gameObject.name);
                tmp = tmp.parent;

            }
        }
        string str = "";
        if (strList.Count >= 2)
        {
            for (int i = 2; i < strList.Count; ++i)
            {
                if (i < strList.Count - 1)
                    str += strList[i] + "/";
                else
                    str += strList[i];
            }
        }

        Debug.Log(str);
        GUIUtility.systemCopyBuffer = str;
    }

    [MenuItem("GameObject/复制完整路径", false, 0)]
    static void CopyGameObjectAllPath()
    {
        Transform select = Selection.activeTransform;
        string _name = select.gameObject.name;
        Transform tmp = null;
        List<string> strList = new List<string>();
        if (null != select.parent)
        {
            tmp = select;
            while (tmp != null)
            {
                strList.Insert(0, tmp.gameObject.name);
                tmp = tmp.parent;

            }
        }
        string str = "";
        if (strList.Count > 0)
        {
            for (int i = 0; i < strList.Count; ++i)
            {
                if (i < strList.Count - 1)
                    str += strList[i] + "/";
                else
                    str += strList[i];
            }
        }

        Debug.Log(str);
        GUIUtility.systemCopyBuffer = str;
    }



    [MenuItem("GameObject/复制LocalPostion", false, 0)]
    static void CopyLocalPos()
    {
        Transform select = Selection.activeTransform;
        Vector3 tmp = select.localPosition;
        string str ="pos = "+ Convert.ToString(tmp.x) + "," + Convert.ToString(tmp.y) + "," + Convert.ToString(tmp.z);

        Quaternion tmp1 = select.localRotation;
        str += " qua = " + Convert.ToString(tmp1.x) + "," + Convert.ToString(tmp1.y) + "," + Convert.ToString(tmp1.z) + "," + Convert.ToString(tmp1.w);

        Vector3 angel = select.localEulerAngles;
        str += " angel = " + Convert.ToString(angel.x) + "," + Convert.ToString(angel.y) + "," + Convert.ToString(angel.z);

        Debug.Log(str);
        GUIUtility.systemCopyBuffer = str;
    }

    [MenuItem("GameObject/复制BoxCollider参数", false, 0)]
    static void CopyBoxColliderParam()
    {
        Transform select = Selection.activeTransform;
        BoxCollider bc = select.gameObject.GetComponent<BoxCollider>();
        if (null == bc)
            bc = select.gameObject.AddComponent<BoxCollider>();



        Vector3 tmp = bc.center;
        string str ="center = "+ Convert.ToString(tmp.x) + "," + Convert.ToString(tmp.y) + "," + Convert.ToString(tmp.z);

        Vector3 angel = bc.size;
        str += " size = " + Convert.ToString(angel.x) + "," + Convert.ToString(angel.y) + "," + Convert.ToString(angel.z);

        Debug.Log(str);
        GUIUtility.systemCopyBuffer = str;
    }

    [MenuItem("GameObject/添加BoxCollider碰撞", false, 0)]
    static void CreateBoxCollider()
    {
        Transform select = Selection.activeTransform;

        Vector3 pos = select.localPosition;
        Quaternion rotate = select.localRotation;
        Vector3 scale = select.localScale;

        select.position = Vector3.zero;
        select.rotation = Quaternion.identity;
        select.localScale = Vector3.one;

        float lossyScaleX = select.lossyScale.x;
        float lossyScaleY = select.lossyScale.y;
        float lossyScaleZ = select.lossyScale.z;

        BoxCollider bc = select.gameObject.GetComponent<BoxCollider>();
        if (null == bc)
            bc = select.gameObject.AddComponent<BoxCollider>();

        Renderer[] renderers = select.GetComponentsInChildren<Renderer>(true);
        if (renderers.Length > 0)
        {
            Bounds bounds = renderers[0].bounds;

            for (int i = 1; i < renderers.Length; i++)
            {
                Renderer mr = renderers[i];
                bounds.Encapsulate(mr.bounds);
            }
            
            bc.center = new Vector3(QuZheng(bounds.center.x / lossyScaleX), QuZheng(bounds.center.y / lossyScaleY), QuZheng(bounds.center.z / lossyScaleZ));
            bc.size = new Vector3(QuZheng(bounds.size.x / lossyScaleX), QuZheng(bounds.size.y / lossyScaleY), QuZheng(bounds.size.z / lossyScaleZ));
        }

        select.localPosition = pos;
        select.localRotation = rotate;
        select.localScale = scale;
    }
    
    private static float QuZheng(float f)
    {
        return Mathf.Round(f * 10000f) / 10000f;
    }
}


